package com.fwj.portal.web.business.product;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.model.PageRequest;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.portal.api.business.product.BusinessProductService;
import com.fwj.portal.entity.product.BusinessProductInfo;
import com.fwj.user.entity.user.UserBaseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: fwj
 * @Data: 2022/5/5 14:15
 * @Version 1.0
 * @Description:
 */


@RestController
@RequestMapping("/business/product")
public class BusinessProductController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessProductController.class);

    @Autowired
    private BusinessProductService businessProductService;

    /**
     * @Description TODO  保存产品信息
     * @Author fwj
     * @Createtime 2022/5/5
     */
    @PostMapping("/saveProduct")
    public ResponseDTO saveProduct(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            BusinessProductInfo businessProductInfo = buildBusinessProduct(jsonObject,userBaseInfo);
            //解析转化数据并转化
            return businessProductService.saveProduct(businessProductInfo);
        } catch (Exception e){
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }

    private BusinessProductInfo buildBusinessProduct(JSONObject jsonObject,UserBaseInfo userBaseInfo) throws Exception{
        BusinessProductInfo businessProductInfo = JSONObject.parseObject(jsonObject.toJSONString(), BusinessProductInfo.class);
        businessProductInfo.setCreator(userBaseInfo.getId());
        return businessProductInfo;
    }

    /**
     * @Description TODO  删除产品信息
     * @Author fwj
     * @Createtime 2022/5/5
     */
    @PostMapping("/deleteProduct")
    public ResponseDTO deleteProduct(@RequestBody JSONObject jsonObject, @LoginUser UserBaseInfo userBaseInfo){
        try {
            //解析转化数据并转化
            return businessProductService.deleteProduct();
        } catch (Exception e){
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }

    /**
     * @Description TODO  查询产品列表信息
     * @Author fwj
     * @Createtime 2022/5/5
     */
    @PostMapping("/queryProductList")
    public ResponseDTO queryProductList(@RequestBody JSONObject jsonObject){
        try {
            //解析转化数据并转化
            PageRequest pageRequest = JSONObject.parseObject(jsonObject.toJSONString(),PageRequest.class);
            return businessProductService.queryProductList(pageRequest);
        } catch (Exception e){
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }

    /**
     * @Description TODO  获取产品详情信息
     * @Author fwj
     * @Createtime 2022/5/5
     */
    @PostMapping("/getProductDetails")
    public ResponseDTO getProductDetails(@RequestBody JSONObject jsonObject){
        try {
            //解析转化数据并转化
            return businessProductService.getProductDetails();
        } catch (Exception e){
            LOGGER.error(e.toString());
            return new ResponseDTO().fail();
        }
    }
}
